<?php

/*

    MMetaverse - Open Metaverse Access Library

    Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

    http://www.maryjanesoft.com
    http://www.marlonj.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    $Id$

*/

/**
 * @package mjs.mmetaverse.packet
 */

require_once 'MPacket.class.php';

/**
 * 
 * Allow the construction of network packets
 *
 * Message List :
 * http://wiki.secondlife.com/wiki/Messages
 */
class MPackets {

    /**
     * Create UseCircuitCode packet
     * http://wiki.secondlife.com/wiki/UseCircuitCode
     *
     * @param integer $circuitCode
     * @param string $agentID
     * @param string $sessionID
     */
    public static function createUseCircuitCodePacket(
        $circuitCode, $agentID, $sessionID) {

        // Create packet
        $packet = new MPacket(0x03,MPacket::FREQUENCY_LOW);
        $packet->addU32($circuitCode);
        $packet->addUUID($sessionID);
        $packet->addUUID($agentID);

        // Return the packet
        return $packet;
    }

    /**
     * Create Complete Agent Movement packet
     * http://wiki.secondlife.com/wiki/CompleteAgentMovement
     *
     * @param integer $circuitCode
     * @param string $agentID
     * @param string $sessionID
     */
    public static function createCompleteAgentMovementPacket(
        $circuitCode, $agentID, $sessionID) {

        // Create packet
        $packet = new MPacket(0xF9,MPacket::FREQUENCY_LOW);
        $packet->addUUID($agentID);
        $packet->addUUID($sessionID);
        $packet->addU32($circuitCode);

        // Return the packet
        return $packet;
    }

    /**
     * Create Logout Request
     * http://wiki.secondlife.com/wiki/LogoutRequest
     *
     * @param string $agentID
     * @param string $sessionID
     */
    public static function createLogoutRequest($agentID, $sessionID) {

        // Create packet
        $packet = new MPacket(0xFC,MPacket::FREQUENCY_LOW);
        $packet->addUUID($agentID);
        $packet->addUUID($sessionID);

        // Return the packet
        return $packet;
    }

    /**
     * Create a AgentUpdate packet
     * http://wiki.secondlife.com/wiki/AgentUpdate
     *
     * @param string $agentID
     * @param string $sessionID
     * @param MQuaternion $bodyRotation
     * @param MQuaternion $headRotation
     * @param byte $state
     * @param MVector3 $cameraCenter
     * @param MVector3 $cameraAtAxis
     * @param MVector3 $cameraLeftAxis
     * @param MVector3 $cameraUpAxis
     * @param float far
     * @param integer $controlFlags
     * @param byte $flags 
     */
    public static function createAgentUpdate($agentID, $sessionID,
        MQuaternion $bodyRotation, MQuaternion $headRotation, $state,
        MVector3 $cameraCenter, MVector3 $cameraAtAxis,
        MVector3 $cameraLeftAxis, $far, $controlFlags, $flags) {
        
        // Create packet
        $packet = new MPacket(0x04,MPacket::FREQUENCY_HIGH);
        $packet->addUUID($agentID);
        $packet->addUUID($sessionID);
        $packet->addQuaternion($bodyRotation);
        $packet->addQuaternion($headRotation);
        $packet->addU8($state);
        $packet->addVector3($cameraCenter);
        $packet->addVector3($cameraAtAxis);
        $packet->addVector3($cameraLeftAxis);
        $packet->addF32($far);
        $packet->addU32($controlFlags);
        $packet->addU8($state);

        // Return the packet
        return $packet;
    }

    /**
     * Create PacketACK
     * http://wiki.secondlife.com/wiki/PacketAck
     *
     * @param integer Packet Number
     * @return MPacket
     */
    public static function createPacketAck($packetNumber)
    {
        // Create packet
        $packet = new MPacket(0xFB,MPacket::FREQUENCY_FIXED);
        $packet->addU32($packetNumber);
        
        // Return the packet
        return $packet;
    }

    /**
     * Create CompletePingCheck
     * http://wiki.secondlife.com/wiki/CompletePingCheck
     *
     * @param integer $pingID
     * @return MPacket
     */
    public static function createCompletePingCheck($pingID)
    {
        // Create packet
        $packet = new MPacket(0x02,MPacket::FREQUENCY_HIGH);
        $packet->addU8($pingID);

        // Return the packet
        return $packet;
    }
}

?>
